package com.hl.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.UserVue;
import com.hl.sfzy.entity.course.Course;
import com.hl.sfzy.service.UserManageService;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @program: sfzy
 * @description: 孙源源-Vue用户管理
 * @author: Syy
 * @create: 2024-09-11 15:27
 **/
@RestController
@RequestMapping("/Manage")
public class UserManageController {
    @Autowired
    private UserManageService userManageService;

    @GetMapping("/select")
    public R<PageInfo<UserVue>> selectAll(Integer pageNo, Integer pageSize) {
        PageInfo<UserVue> userVuePageInfo = userManageService.selectAll(pageNo, pageSize);
        return R.success(userVuePageInfo);
    }

    @PostMapping("/insert")
    public R<UserVue> courseInsert(UserVue userVue) {
        int i = userManageService.insertUser(userVue);
        return i > 0 ? R.success(userVue) : R.failure("500");
    }

    @PostMapping("/fileup")
    public R<String> upload(MultipartFile file) throws IOException {
        String path = "D:\\picture\\";
        File file1 = new File(path);
        if (!file1.exists()) {
            file1.mkdir();
        }
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID().toString() + ext;
        path = path + filename;
        file.transferTo(new File(path));
        return R.success(filename);
    }

    @GetMapping("/downImg")
    public void down(String file, HttpServletResponse resp) throws Exception {
        String path = "d:\\picture\\" + file;
        FileInputStream inputStream = new FileInputStream(path);
        ServletOutputStream outputStream = resp.getOutputStream();
        byte[] bytes = new byte[1024];
        int i = inputStream.read(bytes);
        while (i != -1) {
            outputStream.write(bytes, 0, i);
            i = inputStream.read(bytes);
        }
        outputStream.close();
        inputStream.close();
    }

    @DeleteMapping("/delete")
    public R<Integer> courseDelete(Long id) {
        int i = userManageService.deleteUser(id);
        return R.success(i);
    }

    @PutMapping("/update")
    public R<UserVue> courseUpdate(UserVue userVue) {
        int i = userManageService.updateUser(userVue);
        return R.success(userVue);
    }

    @GetMapping("/selectlike")
    public R<PageInfo<UserVue>> selectLikeUsername(Integer pageNo, Integer pageSize, String username) {
        PageInfo<UserVue> userVuePageInfo = userManageService.selectLikeUsername(pageNo, pageSize, username);
        return R.success(userVuePageInfo);
    }
}
